﻿/*************************************************************************
 * Created by OhZee Creative, LLC
 * 2013
 * This code is licensed under Microsoft Public License (Ms-PL)
 * http://opensource.org/licenses/ms-pl
 * http://ozframework.codeplex.com/
 *************************************************************************/

namespace OhZeeCreative.Helpers
{
	using Windows.UI.Xaml;
	using Windows.UI.Xaml.Controls;
	using Windows.UI.Xaml.Media;

	public static class ResourceHelper
	{
		public static object GetResource(string resourceKey, FrameworkElement element, bool recurseUp = false)
		{
			object resource = null;

			while (element != null)
			{
				if (element.Resources.TryGetValue(resourceKey, out resource) || !recurseUp)
				{
					break;
				}

				element = VisualTreeHelper.GetParent(element) as FrameworkElement;
				//element = element.Parent as FrameworkElement;
			}

			return resource;
		}

		public static object GetLocalResource(this FrameworkElement element, string resourceKey)
		{
			return GetResource(resourceKey, element, recurseUp: false);
		}

		public static object GetPageResource(string resourceKey)
		{
			Frame frame = Window.Current.Content as Frame;
			if (frame != null)
			{
				return GetResource(resourceKey, frame.Content as Page);
			}

			return null;
		}
	}
}
